/*
 * chatinputtext.cpp - line edit widget for chat
 * Copyright (C) 2009  Sergei Stolyarov
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Kajim::ChatInputText widget implements basic functions required
 * by chat text entry widget: history navigation, autocompletion etc
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <QtGui>
#include <QtDebug>
#include "chatinputtext.h"

struct Kajim::ChatInputText::Private
{
    QStringList history;

    // saves current position in the history, required for history navigation
    // position calculates *from the bottom* of list
    int historyPos;
    QString currentText;
    QString tmpMessageSlot;
    QShortcut * sendShortcut;
    QShortcut * historyPrevShortcut;
    QShortcut * historyNextShortcut;
};

Kajim::ChatInputText::ChatInputText(QWidget * parent)
    : QLineEdit(parent)
{
    d = new Private;

    // TODO: set limit to messages history size
    d->historyPos = -1;

    // create shortcuts
    d->sendShortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Return), this, 0, 0, Qt::WidgetShortcut);
    d->historyPrevShortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Up), this, 0, 0, Qt::WidgetShortcut);
    d->historyNextShortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Down), this, 0, 0, Qt::WidgetShortcut);

    // connect slots
    connect(d->sendShortcut, SIGNAL(activated()), this, SLOT(sendText()));
    connect(d->historyPrevShortcut, SIGNAL(activated()), this, SLOT(historyPrev()));
    connect(d->historyNextShortcut, SIGNAL(activated()), this, SLOT(historyNext()));
    connect(this, SIGNAL(textEdited(const QString &)), this, SLOT(textEdited(const QString &)));
}

Kajim::ChatInputText::~ChatInputText()
{
    delete d;
}

void Kajim::ChatInputText::sendText()
{
    QString t(text());
    clear();

    emit textSent(t);
    if (d->history.isEmpty() || d->history.last() != t) {
        d->history.append(t);
        d->currentText.clear();
    }
}

/**
 * \brief Display previous message from history
 *
 * If widget already contains some text it will be remembered in the temporary
 * data slot but won't be permanently added to history.
 */
void Kajim::ChatInputText::historyPrev()
{
    if (d->historyPos == 0) {
        // i.e. this is a first step, so remember current content of widget
        d->tmpMessageSlot = text();
    }
    int history_len = d->history.length();
    int pos = history_len - 1 - d->historyPos;
    if (pos < 0) {
        pos = 0;
    }

    const QString & m = d->history[pos];
    setText(m);
    d->historyPos = history_len - pos;
    if (d->historyPos >= history_len) {
        d->historyPos--;
    }
}

void Kajim::ChatInputText::historyNext()
{
    if (d->historyPos == 0) {
        // take saved value
        setText(d->tmpMessageSlot);
        return;
    }

    int history_len = d->history.length();
    int pos = history_len - d->historyPos;
    if (pos >= history_len) {
        pos = history_len - 1;
    }

    const QString & m = d->history[pos];
    d->historyPos = history_len - pos - 1;
    setText(m);
}

void Kajim::ChatInputText::textEdited(const QString & text)
{
    d->historyPos = 0;
}
